﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ChatClient
{
    public partial class LoginForm : Form
    {
        private MainWindow mainWindow;
        String username = " ";

        public delegate void LoginDelegate(String message);

        public LoginForm(MainWindow _mainWindow)
        {
            this.mainWindow = _mainWindow;
            InitializeComponent();
        }

        public void showWarning(String warningMsg)
        {
            BeginInvoke((MethodInvoker)delegate
            {
                warningLbl.Text = warningMsg;
            });
        }


        private void loginBtn_Click(object sender, EventArgs e)
        {
            username = usernameField.Text;
            if (username.Length < 1) 
            {
                showWarning("Username cannot be blank");
                //warningLbl.Text = "Username cannot be blank";
            }
            else
            {
                showWarning("Connecting to server...");
                //warningLbl.Text = "Connecting to server...";
                System.Diagnostics.Debug.WriteLine("connecting");
                mainWindow.loginClient(username);
                System.Diagnostics.Debug.WriteLine("login sent");
            }
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            mainWindow.Close();
            this.Close();
        }

        private void LoginForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            mainWindow.Close();
            this.Close();
        }
    }
}
